package com.eportal.action.background;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.action.background.pojo.Page;
import com.eportal.domain.AppStore;
import com.eportal.domain.AppstoreCustom;
import com.eportal.domain.Sysopt;
import com.eportal.domain.TAgent;
import com.eportal.domain.TCommunity;
import com.eportal.domain.TMirrorAgent;
import com.eportal.exception.NotUniqueException;
import com.eportal.service.AppStoreCustomService;
import com.eportal.service.AppStoreService;
import com.eportal.service.TAgentService;
import com.eportal.service.TCommunityService;
import com.eportal.service.TMirrorAgentService;
import com.eportal.util.BeanMapUtils;
import com.eportal.util.Config;
import com.eportal.util.HomePageConfig;
import com.eportal.util.UploadUtil;
import com.eportal.util.ValidateUtil;
import com.eportal.util.WebUtil;
import com.framework.jqgrid.JqGridPager;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;

/**
 * 应用定制管理
 * @author wangk
 *
 */
public class AppStoreCustomAction extends BaseAction implements ModelDriven<AppstoreCustom> {
	
    /**
	 * @author wangk
	 */
	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(AppStoreCustomAction.class.getName());

    private Map<String, Object> result = new HashMap<String, Object>();

    @Autowired
    private AppStoreService appStoreService;
    @Autowired
    private AppStoreCustomService appStoreCustomService;

    
    /**
     * 保存
     */
    public void edit(){
    	AppstoreCustom appstoreCustom = getModel();
    	AppstoreCustom bean = new AppstoreCustom();
    	Sysopt  user = (Sysopt)getSession().getAttribute("user");
		String msg = ValidateUtil.validate(appstoreCustom);
		result = new HashMap<String, Object>();
		if (msg == null || msg.trim().length() == 0) {
			try {
				AppstoreCustom custom = appStoreCustomService.getByAgtcode(user.getAgtCode());
				if(custom!=null){
					bean = custom;
				}
				File file = getImgLogoFile();
				if(file!=null){
					Map<String,String> m = uploadImg(file,getImgLogoFileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgLogo(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
				file = getImgLaunch1File();
				if(file!=null){
					Map<String,String> m = uploadImg(file,getImgLaunch1FileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgLaunch1(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
				file = getImgLaunch2File();
				if(file!=null){
					Map<String,String> m = uploadImg(file, getImgLaunch2FileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgLaunch2(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
				file = getImgLaunch3File();
				if(file!=null){
					Map<String,String> m = uploadImg(file,getImgLaunch3FileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgLaunch3(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
				file = getImgLaunch4File();
				if(file!=null){
					Map<String,String> m = uploadImg(file,getImgLaunch4FileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgLaunch4(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
				file = getImgLaunch5File();
				if(file!=null){
					Map<String,String> m = uploadImg(file, getImgLaunch5FileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgLaunch5(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
				file = getImgLaunch6File();
				if(file!=null){
					Map<String,String> m = uploadImg(file,getImgLaunch6FileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgLaunch6(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
				file = getImgLaunch7File();
				if(file!=null){
					Map<String,String> m = uploadImg(file, getImgLaunch7FileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgLaunch7(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
				file = getImgLaunch8File();
				if(file!=null){
					Map<String,String> m = uploadImg(file,getImgLaunch8FileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgLaunch8(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
				file = getImgLaunch9File();
				if(file!=null){
					Map<String,String> m = uploadImg(file,getImgLaunch9FileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgLaunch9(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				file = getImgAdFile();
				if(file!=null){
					Map<String,String> m = uploadImg(file,getImgAdFileFileName(), ",png,");
					if("1".equals(m.get("code"))){
						bean.setImgAd(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
				file = getAboutUsFile();
				if(file!=null){
					Map<String,String> m = uploadImg(file,getAboutUsFileFileName(), ",doc,docx,");
					if("1".equals(m.get("code"))){
						bean.setAboutUs(m.get("url"));
					}else{
					/*	result.putAll(m);
						responseBody(result);
						return;*/
					}
				}
				
		    	bean.setCname(appstoreCustom.getCname());
		    	bean.setThemeColor(appstoreCustom.getThemeColor());
		    	
		    	bean.setCusername(user.getSid().toString());
		    	bean.setAgtcode(user.getAgtCode());
		    	bean.setCtime(new Timestamp(System.currentTimeMillis()));
		    	appStoreCustomService.save(bean);
		    	result.put("code", "1");
				result.put("msg", "保存成功");
			} catch (Exception e) {
				log.error(this, e);
				result.put("code", "-1");
				result.put("msg", "保存失败");
			}
		} else {
			result.put("code", 0);
			result.put("msg", msg);
		}
		responseBody(result);
    }
    
    public void view(){
    	Sysopt  user = (Sysopt)getSession().getAttribute("user");
		try {
			AppstoreCustom custom = appStoreCustomService.getByAgtcode(user.getAgtCode());
			Map map = BeanMapUtils.toMap(custom);
			map.remove("cusername");
			result.put("code", "1");
			result.put("msg", "success");
			result.put("data", map);
		} catch (Exception e) {
			result.put("code", "1");
			result.put("msg", "success");
			log.error(this, e);
		}
		getRequest().setAttribute("", "");
		responseBody(result);
    }
    
    private Map<String, String> uploadImg(File file, String fileName,String Limitext) {
    	Map result = new HashMap<String, String>();
    	result.put("code", "-1");
    	try {
	    	if (file.length() > 5120000) {
				result.put("msg", "不能大于5M");
				return result;
			}
			if (StringUtils.isBlank(fileName) || Limitext.indexOf(","+FilenameUtils.getExtension(fileName)+",")==-1) {
				result.put("msg", "请上传指定格式的文件");
				return result;
			}
			String url = File.separator+"upload"+File.separator+"file"+File.separator;
	        String destDir = ServletActionContext.getRequest().getServletContext().getRealPath(url);
	        if (!new File(destDir).exists()) {
				new File(destDir).mkdirs();
			}
	        String filename = "appstoreCustom_"+UUID.randomUUID().toString().replace("-", "") + "."+FilenameUtils.getExtension(fileName);
			FileUtils.copyFile(file, new File(destDir, filename));
			result.put("code", "1");
			result.put("url",WebUtil.getReqPreUrl(getRequest())+(url+filename).replace(File.separator, "/"));
		} catch (IOException e) {
			log.error(this, e);
			result.put("msg", "上传传到服务器失败，请重试");
		}
    	return result;
	}
    
    /**
	 * 列表显示
	 *
	 * @return
	 */

	public String listView() {
		
		return SUCCESS;
	}
	
	public String list() {
		Map<String,String> params = new HashMap<String,String>();
		StringBuffer where = new StringBuffer("");
		Sysopt  user= (Sysopt)getSession().getAttribute("user");
		int userRole =  (Integer) getSession().getAttribute("userRole");
		if (userRole == 5 || userRole == 6) {
				return "error";
		}
		if (userRole == 1 || userRole == 2) {
			where.append("  ");
		}
		if (userRole == 21) {
			where.append(" and (a.sysoptId=");
			where.append(user.getSid());
			where.append(")");
		}
		if (userRole == 3 || userRole == 4) {
			where.append(" and (t.agtCode='");
			where.append(user.getAgtCode());
			where.append("')");
		}
		
    	
		Page page = new Page();
		String pageNo = getRequest().getParameter("page");
		if (pageNo != null && pageNo.trim().length() > 0) {
			page.setCurrentPage(Integer.parseInt(pageNo));
		}
		
		page = appStoreCustomService.find(where.toString(), page);
		getRequest().setAttribute("page", page);
		return SUCCESS;
	}
    
    private void error(String msg) {
    	result.put("code", 0);
		result.put("msg", msg);
		responseBody(result);
	}

    /**
     * 应用修改
     */
    public String editView() {
    	try{
    	
    	}catch(Exception e){
    		log.error(this, e);
    	}
        return SUCCESS;
    }
    

   
    private AppstoreCustom appstoreCustom = new AppstoreCustom();

    
    @Override
    public AppstoreCustom getModel() {
        return appstoreCustom;
    }
    
    private File imgLaunch1File;
    
    private String imgLaunch1FileFileName;
    
    private File imgLogoFile;
    
    private String imgLogoFileFileName;

    private File imgLaunch2File;
    
    private String imgLaunch2FileFileName;
    
    private File imgLaunch3File;
    
    private String imgLaunch3FileFileName;
    
    private File imgLaunch4File;
    
    private String imgLaunch4FileFileName;
    
    private File imgLaunch5File;
    
    private String imgLaunch5FileFileName;
    
    private File imgLaunch6File;
    
    private String imgLaunch6FileFileName;
    
    private File imgLaunch7File;
    
    private String imgLaunch7FileFileName;
    
    private File imgLaunch8File;
    
    private String imgLaunch8FileFileName;
    
    private File imgLaunch9File;
    
    private String imgLaunch9FileFileName;
    
    private File aboutUsFile;
    
    private String aboutUsFileFileName;
    
    private File imgAdFile;
    
    private String imgAdFileFileName;
    
    
    
    
	public File getAboutUsFile() {
		return aboutUsFile;
	}

	public void setAboutUsFile(File aboutUsFile) {
		this.aboutUsFile = aboutUsFile;
	}

	public String getAboutUsFileFileName() {
		return aboutUsFileFileName;
	}

	public void setAboutUsFileFileName(String aboutUsFileFileName) {
		this.aboutUsFileFileName = aboutUsFileFileName;
	}

	public File getImgAdFile() {
		return imgAdFile;
	}

	public void setImgAdFile(File imgAdFile) {
		this.imgAdFile = imgAdFile;
	}

	public String getImgAdFileFileName() {
		return imgAdFileFileName;
	}

	public void setImgAdFileFileName(String imgAdFileFileName) {
		this.imgAdFileFileName = imgAdFileFileName;
	}

	public String getImgLogoFileFileName() {
		return imgLogoFileFileName;
	}

	public void setImgLogoFileFileName(String imgLogoFileFileName) {
		this.imgLogoFileFileName = imgLogoFileFileName;
	}

	public String getImgLaunch2FileFileName() {
		return imgLaunch2FileFileName;
	}

	public void setImgLaunch2FileFileName(String imgLaunch2FileFileName) {
		this.imgLaunch2FileFileName = imgLaunch2FileFileName;
	}

	public String getImgLaunch3FileFileName() {
		return imgLaunch3FileFileName;
	}

	public void setImgLaunch3FileFileName(String imgLaunch3FileFileName) {
		this.imgLaunch3FileFileName = imgLaunch3FileFileName;
	}

	public String getImgLaunch4FileFileName() {
		return imgLaunch4FileFileName;
	}

	public void setImgLaunch4FileFileName(String imgLaunch4FileFileName) {
		this.imgLaunch4FileFileName = imgLaunch4FileFileName;
	}

	public String getImgLaunch5FileFileName() {
		return imgLaunch5FileFileName;
	}

	public void setImgLaunch5FileFileName(String imgLaunch5FileFileName) {
		this.imgLaunch5FileFileName = imgLaunch5FileFileName;
	}

	public String getImgLaunch6FileFileName() {
		return imgLaunch6FileFileName;
	}

	public void setImgLaunch6FileFileName(String imgLaunch6FileFileName) {
		this.imgLaunch6FileFileName = imgLaunch6FileFileName;
	}

	public String getImgLaunch7FileFileName() {
		return imgLaunch7FileFileName;
	}

	public void setImgLaunch7FileFileName(String imgLaunch7FileFileName) {
		this.imgLaunch7FileFileName = imgLaunch7FileFileName;
	}

	public String getImgLaunch8FileFileName() {
		return imgLaunch8FileFileName;
	}

	public void setImgLaunch8FileFileName(String imgLaunch8FileFileName) {
		this.imgLaunch8FileFileName = imgLaunch8FileFileName;
	}

	public String getImgLaunch9FileFileName() {
		return imgLaunch9FileFileName;
	}

	public void setImgLaunch9FileFileName(String imgLaunch9FileFileName) {
		this.imgLaunch9FileFileName = imgLaunch9FileFileName;
	}

	public String getImgLaunch1FileFileName() {
		return imgLaunch1FileFileName;
	}

	public void setImgLaunch1FileFileName(String imgLaunch1FileFileName) {
		this.imgLaunch1FileFileName = imgLaunch1FileFileName;
	}

	public File getImgLaunch1File() {
		return imgLaunch1File;
	}

	public void setImgLaunch1File(File imgLaunch1File) {
		this.imgLaunch1File = imgLaunch1File;
	}

	public File getImgLogoFile() {
		return imgLogoFile;
	}

	public void setImgLogoFile(File imgLogoFile) {
		this.imgLogoFile = imgLogoFile;
	}

	public File getImgLaunch2File() {
		return imgLaunch2File;
	}

	public void setImgLaunch2File(File imgLaunch2File) {
		this.imgLaunch2File = imgLaunch2File;
	}

	public File getImgLaunch3File() {
		return imgLaunch3File;
	}

	public void setImgLaunch3File(File imgLaunch3File) {
		this.imgLaunch3File = imgLaunch3File;
	}

	public File getImgLaunch4File() {
		return imgLaunch4File;
	}

	public void setImgLaunch4File(File imgLaunch4File) {
		this.imgLaunch4File = imgLaunch4File;
	}

	public File getImgLaunch5File() {
		return imgLaunch5File;
	}

	public void setImgLaunch5File(File imgLaunch5File) {
		this.imgLaunch5File = imgLaunch5File;
	}

	public File getImgLaunch6File() {
		return imgLaunch6File;
	}

	public void setImgLaunch6File(File imgLaunch6File) {
		this.imgLaunch6File = imgLaunch6File;
	}

	public File getImgLaunch7File() {
		return imgLaunch7File;
	}

	public void setImgLaunch7File(File imgLaunch7File) {
		this.imgLaunch7File = imgLaunch7File;
	}

	public File getImgLaunch8File() {
		return imgLaunch8File;
	}

	public void setImgLaunch8File(File imgLaunch8File) {
		this.imgLaunch8File = imgLaunch8File;
	}

	public File getImgLaunch9File() {
		return imgLaunch9File;
	}

	public void setImgLaunch9File(File imgLaunch9File) {
		this.imgLaunch9File = imgLaunch9File;
	}
	
	
}
